<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                <FormItem :label="t('xBjline.BJNO')" type="string" v-model="baTable.form.items!.BJNO" prop="BJNO" :input-attr="{ placeholder: t('Please input field', { field: t('xBjline.BJNO') }) ,disabled:true}" />
                <!-- <FormItem
                    :label="t('xBjline.LOADPLACE')"
                    v-model="baTable.form.items!.LOADPLACE"
                    prop="LOADPLACE"
                    type="remoteSelect"
                    :input-attr="{
                        pk: 'id',
                        field: 'nickname',
                        'remote-url':'admin/XBjline/ywtype',
                        placeholder: t('Click Select'),
                    }"
                /> -->
                <el-form-item :label="t('xBjline.LOADPLACE')" prop="LOADPLACE">
                    <el-select v-model="baTable.form.items!.LOADPLACE" placeholder="请选择" filterable style="width:100%">
                        <el-option v-for="item in regins.from" :label="item" :value="item" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('xBjline.DISPLACE')" prop="DISPLACE">
                    <el-select v-model="baTable.form.items!.DISPLACE" placeholder="请选择" filterable style="width:100%">
                        <el-option v-for="item in regins.to" :label="item" :value="item" />
                    </el-select>
                </el-form-item>
                <!-- <FormItem 
                    :label="t('xBjline.DISPLACE')" 
                    type="string" 
                    v-model="baTable.form.items!.DISPLACE" prop="DISPLACE" :input-attr="{ placeholder: t('Please input field', { field: t('xBjline.DISPLACE') }) }" /> -->
                <FormItem :label="t('xBjline.NOTE')" type="string" v-model="baTable.form.items!.NOTE" prop="NOTE" :input-attr="{ placeholder: t('Please input field', { field: t('xBjline.NOTE') }) }" />
                <FormItem :label="t('xBjline.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: t('当前登录人'),disabled:true }" />
                <FormItem :label="t('xBjline.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: t('Please select field'),disabled:true }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject ,watchEffect,onMounted} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import createAxios from '/@/utils/axios'



const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xBjline.OPTIME') })})],
})

const bjno = inject('bjno')
const transData:{
    from:string
    to:string
}|undefined = inject('transData')

watchEffect(()=>{
    if(baTable.form.operate == 'add'){
        baTable.form.items!.BJNO = bjno || '2011BJ0013'
        // console.log(transData)
        baTable.form.items!.LOADPLACE = transData!.from
        baTable.form.items!.DISPLACE = transData!.to
    }
})


onMounted(()=>{
    getRegins()
})

const regins:{
    from:any[]
    to:any[]
} = reactive({
    from:[],
    to:[]
})

const getRegins = ()=>{
    createAxios({
        url:'/admin/XBjline/ywtype',
        params:{
            BJNO:baTable.form.items!.BJNO,
            PORT:'装货地'
        },
        method:'get'
    }).then((res:any)=>{
        regins.from = res.data
    })
    createAxios({
        url:'/admin/XBjline/ywtype',
        params:{
            BJNO:baTable.form.items!.BJNO,
            PORT:'送货地'
        },
        method:'get'
    }).then((res:any)=>{
        regins.to = res.data
    })
}



</script>

<style scoped lang="scss"></style>
